from bag_of_words import BagOfWords
from dataset_builder import DatasetBuilder
from our_agent_comparator import OurAgentComparator
from nearest_neighbor import NearestNeighbor
from agent_gonzales import Gonzales
from time import clock

comparator = OurAgentComparator()
learnTasks = comparator.getLearningTasks()

stemming, stopWords = True, True
minFeatures, maxFeatures, step = 5,50, 10
n_list=[5,6,7,8,9,10,11,12,13,14,15,25,35,45]

classifyTime = 2
learnMinutes = 10



for name, lTask in learnTasks.items():
    for nFeatures in n_list:
        startTime = clock()
        
        bothFile = open('results/both.txt','a')
        regularFile  = open('results/both_regular.txt','a')
        stopwordsVsRegularFile = open('results/bothVsRegular.txt','a')
        
        agentBoth = Gonzales(stemming, stopWords, nFeatures)
        agentNothing = Gonzales(False, False, nFeatures)
        confusionBoth, confusionNothing, mcnemar = comparator.run(
            lTask, agentBoth, agentNothing, 2, learnMinutes*60, 15)
        
        bothFile.write(name+'\t'+str(nFeatures)+'\t'+str(100*confusionBoth.getAccuracy())+'\n')
        regularFile.write(name+'\t'+str(nFeatures)+'\t'+str(100*confusionNothing.getAccuracy())+'\n')
        
        sig = mcnemar.getSignificance()*100
        matrix = mcnemar.matrix
        
        stopwordsVsRegularFile.write(name+'\t')
        stopwordsVsRegularFile.write(str(matrix[True][True])+'\t'+str(matrix[True][False])+'\t')
        stopwordsVsRegularFile.write(str(matrix[False][True])+'\t'+str(matrix[False][False])+'\t')
        stopwordsVsRegularFile.write(str(sig)+'\t')
        
        if matrix[False][True] < matrix[True][False]:
            stopwordsVsRegularFile.write(str(mcnemar.agent1_name)+'\t'+str(mcnemar.agent2_name)+'\n')
        elif matrix[False][True] > matrix[True][False]:
            stopwordsVsRegularFile.write(str(mcnemar.agent2_name)+'\t'+str(mcnemar.agent1_name)+'\n')
        else:
            stopwordsVsRegularFile.write('None\tNone\n')

        bothFile.close()
        regularFile.close()
        stopwordsVsRegularFile.close()
        totalTime = clock()-startTime
        print 'Task:' , name , ", features:" , nFeatures , ", time =" , totalTime